package fdoj.eventInvitations.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fdoj.eventInvitations.dao.ContactDao;
import fdoj.eventInvitations.dao.PersonDao;
import fdoj.eventInvitations.entity.Contact;
import fdoj.eventInvitations.entity.Person;

public class AddressBookController extends HttpServlet {
	private HttpSession session;
	@EJB
	ContactDao contactDao;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			session = request.getSession();
		    Person loggedUser = (Person) request.getSession().getAttribute("loggedUser");
			if (loggedUser == null) {
				response.sendRedirect(response.encodeRedirectURL("./index"));
				return;
			} else {
				List<Contact> contacts=contactDao.findByOwner(loggedUser.getId());
				session.setAttribute("contacts", contacts);
				request.getRequestDispatcher("/addressBook.jsp").forward(
						request, response);
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
		response.sendRedirect(response.encodeRedirectURL("./error.jsp"));
	}

}
